﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using EasySamba.Models;
using Microsoft.AspNetCore.Authorization;

namespace EasySamba.Controllers
{
    [Authorize]
    public class EasyController : Controller
    {
        public IActionResult Index()
        {
            var viewModel = new EasyViewModel();
            var result = Comn.LoadFullConfig();
            if (result.Success)
            {
                viewModel.ConfigNodeList = result.Data.ConfigNodeList;
            }
            return View(viewModel);
        }

        [HttpPost]
        public JsonResult Enable(string name)
        {
            var result = new Result(false, string.Empty);

            var resultLoadFullConfig = Comn.LoadFullConfig();
            if (resultLoadFullConfig.Success)
            {
                var fullConfig = resultLoadFullConfig.Data;
                var node = fullConfig.ConfigNodeList.FirstOrDefault(item => item.Name == name);
                if (node != null)
                {
                    node.IsEnable = true;
                    var adminPassword = Startup.Configuration["Password"];
                    result = Comn.SaveFullConfig(adminPassword, fullConfig);
                }
                else
                {
                    result.Massage = $"没有找到名为 {name} 的共享！";
                }
            }
            else
            {
                result.Massage = resultLoadFullConfig.Massage;
            }

            return Json(result);
        }

        [HttpPost]
        public JsonResult Disable(string name)
        {
            var result = new Result(false, string.Empty);

            var resultLoadFullConfig = Comn.LoadFullConfig();
            if (resultLoadFullConfig.Success)
            {
                var fullConfig = resultLoadFullConfig.Data;
                var node = fullConfig.ConfigNodeList.FirstOrDefault(item => item.Name == name);
                if (node != null)
                {
                    node.IsEnable = false;
                    var adminPassword = Startup.Configuration["Password"];
                    result = Comn.SaveFullConfig(adminPassword, fullConfig);
                }
                else
                {
                    result.Massage = $"没有找到名为 {name} 的共享！";
                }
            }
            else
            {
                result.Massage = resultLoadFullConfig.Massage;
            }

            return Json(result);
        }

        public IActionResult New()
        {
            return View();
        }

        [AcceptVerbs("Get", "Post")]
        public IActionResult VerifyNewName(string name)
        {
            var result = new Result();

            var resultLoadFullConfig = Comn.LoadFullConfig();
            if (resultLoadFullConfig.Success)
            {
                var fullConfig = resultLoadFullConfig.Data;
                if ( fullConfig.ConfigNodeList.Any(item => item.Name == name))
                {
                    result.Massage = $"名称 {name} 已经存在！";
                }
                else
                {
                    result.Success = true;
                }
            }
            else
            {
                result.Massage = resultLoadFullConfig.Massage;
            }

            if (result.Success)
            {
                return Json(true);
            }
            else
            {
                return Json(result.Massage);
            }
        }

        [AcceptVerbs("Get", "Post")]
        public IActionResult VerifyNewPath(string path)
        {
            var result = new Result();

            var resultLoadFullConfig = Comn.LoadFullConfig();
            if (resultLoadFullConfig.Success)
            {
                var fullConfig = resultLoadFullConfig.Data;
                if (fullConfig.ConfigNodeList.Any(item => item.Path == path))
                {
                    result.Massage = $"目录 {path} 已被共享！";
                }
                else
                {
                    if (!System.IO.Directory.Exists(path))
                    {
                        result.Massage = $"目录 {path} 不存在！";
                    }
                    else
                    {
                        result.Success = true;
                    }
                }
            }
            else
            {
                result.Massage = resultLoadFullConfig.Massage;
            }

            if (result.Success)
            {
                return Json(true);
            }
            else
            {
                return Json(result.Massage);
            }
        }

        [HttpPost]
        public IActionResult AddNew(NewViewModel newViewModel)
        {
            var result = new Result(false, string.Empty);

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var newConfigNode = new ConfigNode();
            newConfigNode.IsEnable = true;
            newConfigNode.Name = newViewModel.Name;
            newConfigNode.Path = newViewModel.Path;
            newConfigNode.ReadOnly = newViewModel.ReadOnly;
            newConfigNode.User = newViewModel.User;
            newConfigNode.EveryOne = newViewModel.EveryOne;

            var resultLoadFullConfig = Comn.LoadFullConfig();
            if (resultLoadFullConfig.Success)
            {
                var fullConfig = resultLoadFullConfig.Data;
                fullConfig.ConfigNodeList.Add(newConfigNode);

                var adminPassword = Startup.Configuration["Password"];
                result = Comn.SaveFullConfig(adminPassword, fullConfig);
            }
            else
            {
                result.Massage = resultLoadFullConfig.Massage;
            }

            if (result.Success)
            {
                return RedirectToAction("Index");
            }
            else
            {
                return BadRequest(ModelState);
            }
        }

        public IActionResult Edit(string name)
        {
            var editViewModel = new EditViewModel();
            editViewModel.CanFindName = false;
            editViewModel.Name = name;

            var resultLoadFullConfig = Comn.LoadFullConfig();
            if (resultLoadFullConfig.Success)
            {
                var fullConfig = resultLoadFullConfig.Data;
                var node = fullConfig.ConfigNodeList.FirstOrDefault(item => item.Name == name);
                if (node != null)
                {
                    editViewModel.CanFindName = true;

                    editViewModel.Name = node.Name;
                    editViewModel.Path = node.Path;
                    editViewModel.ReadOnly = node.ReadOnly;
                    editViewModel.User = node.User;
                    editViewModel.EveryOne = node.EveryOne;
                }
            }

            return View(editViewModel);
        }

        [AcceptVerbs("Get", "Post")]
        public IActionResult VerifyEditPath(string name, string path)
        {
            var result = new Result();

            var resultLoadFullConfig = Comn.LoadFullConfig();
            if (resultLoadFullConfig.Success)
            {
                var fullConfig = resultLoadFullConfig.Data;
                if (fullConfig.ConfigNodeList.Any(item => item.Name != name && item.Path == path))
                {
                    result.Massage = $"目录 {path} 已被共享！";
                }
                else
                {
                    if (!System.IO.Directory.Exists(path))
                    {
                        result.Massage = $"目录 {path} 不存在！";
                    }
                    else
                    {
                        result.Success = true;
                    }
                }
            }
            else
            {
                result.Massage = resultLoadFullConfig.Massage;
            }

            if (result.Success)
            {
                return Json(true);
            }
            else
            {
                return Json(result.Massage);
            }
        }

        [HttpPost]
        public IActionResult UpdateEdit(EditViewModel editViewModel)
        {
            var result = new Result(false, string.Empty);

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var resultLoadFullConfig = Comn.LoadFullConfig();
            if (resultLoadFullConfig.Success)
            {
                var fullConfig = resultLoadFullConfig.Data;
                var node = fullConfig.ConfigNodeList.FirstOrDefault(item => item.Name == editViewModel.Name);
                if (node != null)
                {
                    node.Path = editViewModel.Path;
                    node.ReadOnly = editViewModel.ReadOnly;
                    node.User = editViewModel.User;
                    node.EveryOne = editViewModel.EveryOne;
                }

                var adminPassword = Startup.Configuration["Password"];
                result = Comn.SaveFullConfig(adminPassword, fullConfig);
            }
            else
            {
                result.Massage = resultLoadFullConfig.Massage;
            }

            if (result.Success)
            {
                return RedirectToAction("Index");
            }
            else
            {
                return BadRequest(ModelState);
            }
        }

        [HttpPost]
        public JsonResult Delete(string name)
        {
            var result = new Result(false, string.Empty);

            var resultLoadFullConfig = Comn.LoadFullConfig();
            if (resultLoadFullConfig.Success)
            {
                var fullConfig = resultLoadFullConfig.Data;
                var node = fullConfig.ConfigNodeList.FirstOrDefault(item => item.Name == name);
                if (node != null)
                {
                    fullConfig.ConfigNodeList.Remove(node);
                    var adminPassword = Startup.Configuration["Password"];
                    result = Comn.SaveFullConfig(adminPassword, fullConfig);
                }
                else
                {
                    result.Massage = $"没有找到名为 {name} 的共享！";
                }
            }
            else
            {
                result.Massage = resultLoadFullConfig.Massage;
            }

            return Json(result);
        }
    }
}
